//
//  ContentViewController.m
//  KeYin
//
//  Created by Sean Chen on 6/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContentViewController.h"


@implementation ContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithPeriodical:(NSDictionary *)periodical {
    self = [super init];
    if (self) {
        NSMutableArray *photos = [[NSMutableArray alloc] init];
        
        NSString *periodicalID = [periodical objectForKey:@"periodical_id"];
        NSUInteger pagesCount = [[periodical objectForKey:@"pages"] integerValue];
        NSString *pageName;
        NSString *pagePath;
        NSString *thumbName;
        NSString *thumbPath;
        
        for (int i = 0; i < pagesCount; i++) {
            pageName = [NSString stringWithFormat:kFileNamePage, periodicalID, i];
            pagePath = [M13FileManager filePathWithFileName:pageName];
            //UIImage *img = [UIImage imageNamed:pageName]; // DOESN'T WORK IN DEVICE !!!
            UIImage *page = [UIImage imageWithContentsOfFile:pagePath];
            NSString *url = [NSString stringWithFormat:@"documents://%@", pageName];
            [[TTURLCache sharedCache] storeImage:page forURL:url];
            
            thumbName = [NSString stringWithFormat:kFileNameThumb, periodicalID, i];
            thumbPath = [M13FileManager filePathWithFileName:thumbName];
            UIImage *thumb = [UIImage imageWithContentsOfFile:thumbPath];
            NSString *thumbURL = [NSString stringWithFormat:@"documents://%@", thumbName];
            [[TTURLCache sharedCache] storeImage:thumb forURL:thumbURL];
            
            ContentPhoto *photo = [[ContentPhoto alloc] initWithURL:url smallURL:thumbURL size:CGSizeZero];
            [photos addObject:photo];
            [photo release];
        }
        
        self.photoSource = [[ContentPhotoSource alloc] initWithPhotos:photos];
        [self.photoSource setTitle:[periodical objectForKey:@"name"]];
        [photos release];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
